home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Mac Source / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-23  |  6.4 KB  |  331 lines  |  [TEXT/CWIE]

  1. /*
  2. ** File:        Menu.c
  3. **
  4. ** Written by:    Bill Hayden
  5. **                Nikol Software
  6. **
  7. ** Copyright © 1995 Nikol Software
  8. ** All rights reserved.
  9. */
  10.  
  11.  
  12.  
  13. /*****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "Constants.h"
  18. #include "Menu.h"
  19. #include "StringUtils.h"
  20. #include "MacWT.h"
  21. #include "Music.h"
  22.  
  23. #ifndef __BALLOONS__
  24. #include <Balloons.h>
  25. #endif
  26.  
  27. #ifndef __DESK__
  28. #include <Desk.h>
  29. #endif
  30.  
  31. #ifndef __ERRORS__
  32. #include <Errors.h>
  33. #endif
  34.  
  35. #ifndef __FONTS__
  36. #include <Fonts.h>
  37. #endif
  38.  
  39. #ifndef __MEMORY__
  40. #include <Memory.h>
  41. #endif
  42.  
  43. #ifndef __MENUS__
  44. #include <Menus.h>
  45. #endif
  46.  
  47. #ifndef __TOOLUTILS__
  48. #include <ToolUtils.h>
  49. #endif
  50.  
  51.  
  52. /*****************************************************************************/
  53.  
  54.  
  55.  
  56. static Boolean    DoAdjustFileMenu(WindowRef window);
  57. static Boolean    DoAdjustEditMenu(WindowRef window);
  58.  
  59. static void        EnableOrDisableItem(MenuHandle menu, short item, Boolean enable);
  60.  
  61. static Boolean    IsDAWindow(WindowRef window);
  62.  
  63.  
  64. /*****************************************************************************/
  65.  
  66.  
  67.  
  68. void    DoAdjustMenus(void)
  69. {
  70.     WindowRef    window;
  71.     Boolean        redrawMenuBar;
  72.  
  73.     window = FrontWindow();
  74.  
  75.     redrawMenuBar  = DoAdjustFileMenu(window);
  76.     redrawMenuBar |= DoAdjustEditMenu(window);
  77.  
  78.     if (redrawMenuBar)
  79.         DrawMenuBar();
  80. }
  81.  
  82.  
  83.  
  84. /*****************************************************************************/
  85.  
  86.  
  87.  
  88. /* This is called when an item is chosen from the menu bar (after calling
  89. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  90. ** It is good to have both the result of MenuSelect and MenuKey go to one
  91. ** routine like this to keep everything organized. */
  92.  
  93. void    DoMenuCommand(long menuResult)
  94. {
  95.     short            menuID, menuItem, daRefNum;
  96.     Str255            str;
  97.  
  98.  
  99.     menuID = HiWord(menuResult);    /* Use macros for efficiency to get  */
  100.     menuItem = LoWord(menuResult);    /* menu item number and menu number. */
  101.  
  102.     switch (menuID) {
  103.  
  104.         case mApple:
  105.             switch (menuItem) {
  106.                 case kAbout:        /* Bring up alert for About. */
  107.                     {
  108.                     char    dateTimeStr[256], compilerStr[256];
  109.                     
  110.                     #if   applec
  111.                     #define    idStr    "MPW C"
  112.                     #elif __MWERKS__
  113.                     #define    idStr    "Metrowerks"
  114.                     #elif THINK_C || THINK_CPLUS
  115.                     #define    idStr    "THINK C/Symantec C++"
  116.                     #endif
  117.                     
  118.                     ccpy(dateTimeStr, __DATE__);
  119.                     ccat(dateTimeStr, ", ");
  120.                     ccat(dateTimeStr, __TIME__);
  121.                     ccpy(compilerStr, "Compiler: ");
  122.                     ccat(compilerStr, idStr);
  123.                     ccat(compilerStr, 
  124.                     #if    GENERATINGPOWERPC
  125.                     " PowerPC");
  126.                     #else
  127.                     " 68K");
  128.                     #endif
  129.                     c2p(compilerStr);
  130.                     c2p(dateTimeStr);
  131.                     ParamText(gWTVersion, (StringPtr)compilerStr, (StringPtr)dateTimeStr, "\p");
  132.                     Alert(rAboutAlert, nil);
  133.                     #undef    idStr
  134.                     }
  135.                     break;
  136.                     
  137.                 default:            /* All non-About items in this menu are DAs. */
  138.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, str);
  139.                     daRefNum = OpenDeskAcc(str);
  140.                     break;
  141.                 }
  142.             break;
  143.  
  144.         case mFile:
  145.             switch (menuItem)
  146.                 {
  147.                 case kNew:
  148.                 case kNewNetwork:
  149.                     BeginGame();
  150.                     break;
  151.                     
  152.                 case kResumeGame:
  153.                     TogglePause();
  154.                     break;
  155.                     
  156.                 case kShowFPS:
  157.                     {
  158.                     MenuHandle        menu;
  159.  
  160.                     menu = GetMenuHandle(mFile);
  161.                     gShowFPS = !gShowFPS;
  162.                     SetItemMark(menu, kShowFPS, (gShowFPS) ? checkMark : noMark);
  163.                     }
  164.                     break;
  165.  
  166.                 case kUseQuickdraw:
  167.                     {
  168.                     MenuHandle        menu;
  169.  
  170.                     menu = GetMenuHandle(mFile);
  171.                     gUseQuickdraw = !gUseQuickdraw;
  172.                     SetItemMark(menu, kUseQuickdraw, (gUseQuickdraw) ? checkMark : noMark);
  173.                     }
  174.                     break;
  175.                     
  176.                 case kMusic:
  177.                     {
  178.                     MenuHandle        menu;
  179.  
  180.                     menu = GetMenuHandle(mFile);
  181.                     if (Playing)
  182.                         StopMusic();
  183.                     else
  184.                         PlayMusic(kMusicID);
  185.                     SetItemMark(menu, kMusic, (Playing) ? checkMark : noMark);
  186.                     }
  187.                     break;
  188.                     
  189.                 case kDrawFC:
  190.                     {
  191.                     MenuHandle        menu;
  192.  
  193.                     menu = GetMenuHandle(mFile);
  194.                     gDrawFC = !gDrawFC;
  195.                     SetItemMark(menu, kDrawFC, (gDrawFC) ? checkMark : noMark);
  196.                     }
  197.                     break;
  198.  
  199.                 case kQuit:
  200.                     quitting = TRUE;
  201.                     break;
  202.                 }
  203.             break;
  204.  
  205.         case mEdit:
  206.             switch (menuItem)
  207.                 {
  208.                 case kCut:
  209.                 case kCopy:
  210.                 case kPaste:
  211.                 case kClear:
  212.                     SystemEdit(menuItem - 1);
  213.                     break;
  214.                 }
  215.             break;
  216.     }
  217.  
  218.     HiliteMenu(0);        /* Unhighlight what MenuSelect (or MenuKey) hilited. */
  219. }
  220.  
  221.  
  222.  
  223. /*****************************************************************************/
  224.  
  225.  
  226.  
  227. Boolean    DoAdjustFileMenu(WindowRef window)
  228. {
  229.     MenuHandle        menu;
  230.     extern OSErr    gMusicErr;
  231.  
  232.     menu = GetMenuHandle(mFile);
  233.     EnableItem(menu, kQuit);            /* Gotta be able to quit. */
  234.  
  235.     EnableOrDisableItem(menu, kNew, !gGameOn);
  236.     EnableOrDisableItem(menu, kResumeGame, gGameOn);
  237.     EnableOrDisableItem(menu, kMusic, !gMusicErr);
  238.  
  239. #if GENERATINGPOWERPC
  240.     DisableItem(menu, kUseQuickdraw);
  241. #else
  242.     EnableOrDisableItem(menu, kUseQuickdraw, !gTrueColor);
  243. #endif
  244.  
  245.     return(false);
  246. }
  247.  
  248.  
  249.  
  250. /*****************************************************************************/
  251.  
  252.  
  253.  
  254. Boolean    DoAdjustEditMenu(WindowRef window)
  255. {
  256.     MenuHandle        menu;
  257.  
  258.  
  259.     menu = GetMenuHandle(mEdit);
  260.     
  261.     EnableOrDisableItem(menu, kUndo, IsAppWindow(window));
  262.     EnableOrDisableItem(menu, kCut, IsAppWindow(window));
  263.     EnableOrDisableItem(menu, kCopy, IsAppWindow(window));
  264.     EnableOrDisableItem(menu, kPaste, IsAppWindow(window));
  265.     
  266.     if (IsDAWindow(window))
  267.         {
  268.         EnableItem(menu, kUndo);
  269.         EnableItem(menu, kCut);
  270.         EnableItem(menu, kCopy);
  271.         EnableItem(menu, kPaste);
  272.         EnableItem(menu, kClear);
  273.         }
  274.  
  275.     return(false);
  276. }
  277.  
  278.  
  279.  
  280. /*****************************************************************************/
  281.  
  282.  
  283.  
  284.  
  285. /* This function either enables or disables a menu item. */
  286.  
  287. static    void    EnableOrDisableItem(MenuHandle menu, short item, Boolean enable)
  288. {
  289.     if (enable)
  290.         EnableItem(menu, item);
  291.     else
  292.         DisableItem(menu, item);
  293. }
  294.  
  295.  
  296.  
  297.  
  298. /*****************************************************************************/
  299.  
  300.  
  301.  
  302. /* Check to see if a window belongs to a desk accessory. */
  303.  
  304. static    Boolean    IsDAWindow(WindowRef window)
  305. {
  306.     if (window)
  307.         return(GetWindowKind(window) < 0);
  308.     else
  309.         return(false);
  310. }
  311.  
  312.  
  313.  
  314. /*****************************************************************************/
  315.  
  316.  
  317.  
  318. /* Check to see if a window belongs to the application.  If the window pointer
  319. ** passed was nil, then it could not be an application window.  WindowKinds
  320. ** that are negative belong to the system and windowKinds less than userKind
  321. ** are reserved by Apple except for windowKinds equal to dialogKind, which
  322. ** mean it is a dialog. For this app, we use only dialogs for our windows. */
  323.  
  324. static    Boolean    IsAppWindow(WindowRef window)
  325. {
  326.     if (window)
  327.         return(GetWindowKind(window) == dialogKind);
  328.     else
  329.         return(false);
  330. }
  331.